package com.sne.airways.system.controller;

import com.sne.airways.basecore.controller.BaseController;
import com.sne.airways.basecore.exceptions.AppException;
import com.sne.airways.system.constants.Constant;
import com.sne.airways.system.constants.StatusType;
import com.sne.airways.system.dto.TdOperationDTO;
import com.sne.airways.system.service.OperationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: 卞京帅{bjslogin@gmail.com}
 * Date: 12-11-17
 * Time: 下午5:54
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/system/operation")
public class OperationController extends BaseController {

    @Autowired
    private OperationService operationService;

    @RequestMapping("/getTopLevel")
    @ResponseBody
    public void initOperation(){
        Set<TdOperationDTO> opSet = operationService.queryPrivateOperationTree("-1", StatusType.启用.getValue());
        renderJsonArray(opSet);
    }
    @RequestMapping("/queryOperationTree")
    @ResponseBody
    public void queryOperationTree(String operationId){
        Set<TdOperationDTO> opSet = operationService.queryOperationTree(operationId, null);
        renderJsonArray(opSet);
    }
    @RequestMapping("/saveOperation")
    @ResponseBody
    public void saveOperation(String opType,String operationId, String parentId, String menuName, String menuView, String menuController,
                              int menuIndex, int menuLevel, int version, String status, String iconCls, String expandedCls, String collapsedCls){
        if(StringUtils.isBlank(opType)){
            throw new AppException("操作类型不能为空!");
        }
        if(StringUtils.equals(Constant.ADD,opType)){
            if(StringUtils.isBlank(menuName)||StringUtils.isBlank(parentId)){
                throw new AppException("菜单名称或者父级ID不能为空!");
            }
            TdOperationDTO tdOperationDTO = operationService.saveOperation(operationId,parentId,menuName,menuView,menuController,
                    menuIndex,menuLevel,version,status,iconCls, expandedCls,collapsedCls);
            renderSuccess(tdOperationDTO);
        }else if(StringUtils.equals(Constant.EDIT,opType)){
            if(StringUtils.isBlank(menuName)||StringUtils.isBlank(parentId)){
                throw new AppException("获取参数出错");
            }
            TdOperationDTO tdOperationDTO = operationService.updateOperation(operationId,parentId,menuName,menuView,menuController,
                    menuIndex,menuLevel,version,status,iconCls, expandedCls,collapsedCls);
            renderSuccess(tdOperationDTO);
        }

    }
    @RequestMapping("/deleteOperation")
    @ResponseBody
    public void deleteOperation(String operationId, int version){
        try {
            if(StringUtils.isBlank(operationId)){
                throw new AppException("菜单ID不能为空!");
            }
            operationService.deleteOperation(operationId,version);
            renderSuccess();
        } catch (AppException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    /**
     * 修改菜单排序
     * @param operationId
     * @param parentId
     * @param oldParentId
     * @param menuIndex
     * @param oldMenuIndex
     * @param menuLevel
     * @param oldMenuLevel
     * @param version
     */
    @RequestMapping("/changeOperationIndex")
    @ResponseBody
    public void changeOperationIndex(String operationId, String parentId, int menuIndex,
                                     int menuLevel, String oldParentId,int oldMenuIndex,  int oldMenuLevel, int version){
        if(StringUtils.isBlank(operationId)||StringUtils.isBlank(parentId)
                ||StringUtils.isBlank(oldParentId)|| StringUtils.isBlank(String.valueOf(menuIndex))
                || StringUtils.isBlank(String.valueOf(menuLevel))|| StringUtils.isBlank(String.valueOf(oldMenuIndex))
                || StringUtils.isBlank(String.valueOf(oldMenuLevel))|| StringUtils.isBlank(String.valueOf(version))){
            throw new AppException("获取参数出错");
        }
        operationService.changeOperationIndex(operationId,parentId,menuIndex,menuLevel,oldParentId,oldMenuIndex,oldMenuLevel,version);
        renderSuccess();
    }
}
